package com.zx.dsptuneup_TriMic.Util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.util.Log;

import java.io.IOException;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.FutureTask;

/**
 * Created by window on 2018/3/7.
 */

public class NetWorkUtils {

    private NetWorkUtils() {}

    /**
     * 判断网络是否可用
     *
     * @param context
     * @return
     */
    public static boolean isNetWorkAvailable(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    }

    /**
     * 检测wifi是否连接
     *
     * @return
     */
    public static boolean isWifiConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI;
    }

    /**
     * 检测3G是否连接
     *
     * @return
     */
    public static boolean is3gConnected(Context context) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_MOBILE;
    }

    // ping 127.0.0.1 -n 10 -w 4  执行10次 超时4秒
    public static boolean pingHost(String str)  {  //str  为要ping的IP地址
        ExecutorService service = Executors.newSingleThreadExecutor();
        Callable<Boolean> readDataCallable = new Callable<Boolean>() {
            @Override
            public Boolean call() throws Exception {
                boolean result = false;
                try {
                    Process p = Runtime.getRuntime().exec("ping -c 1 -w 4 " + str);
                    int status = p.waitFor();
                    if (status == 0) {
                        result = true;
                        Log.e("","ip 是通的");
                    }
                    else {
                        Log.e("","ip 不通！！！");
                        result = false;
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }catch (IOException e) {

                }
                return result;
            }
        };
        FutureTask<Boolean> pingFutureTask = new FutureTask<>(readDataCallable);
        service.execute(pingFutureTask);
        try{
            return  pingFutureTask.get();
        }catch (InterruptedException e){
            e.printStackTrace();
            return true;
        }catch (ExecutionException e) {
            e.printStackTrace();
            return true;
        }
    }

}
